package net.zhengxinyun.performance.service.news;

import net.zhengxinyun.performance.Util.StringUtils;
import net.zhengxinyun.performance.api.entity.SysFile;
import net.zhengxinyun.performance.api.service.SysFileService;
import net.zhengxinyun.performance.entity.NewsEntity;
import net.zhengxinyun.performance.filter.NewsFilter;
import net.zhengxinyun.performance.mapper.news.NewsEntityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.*;

/**
 * Created by hyp1202 on 2018/12/7.
 */
@Service
public class NewsServiceImpl implements NewsService {

    private NewsEntityMapper newsEntityMapper;
    private SysFileService sysFileService;

    @Autowired
    public NewsServiceImpl(NewsEntityMapper newsEntityMapper, SysFileService sysFileService) {
        this.newsEntityMapper = newsEntityMapper;
        this.sysFileService = sysFileService;
    }


    @Override
    public String insert(NewsEntity newsEntity) {
        int i = newsEntityMapper.insert(newsEntity);
        if (i == 0) {
            return "添加失败,请联系管理员";
        }
        return "添加成功";
    }

    @Override
    @Transactional
    public String delete(NewsEntity newsEntity) {
        NewsEntity entity = newsEntityMapper.selectByPrimaryKey(newsEntity.getId());
        if (StringUtils.isNotBlank(entity.getUrl())) {
            List<String> list = new ArrayList<>();
            String url = entity.getUrl();
            list.add(url);
            String str = sysFileService.delete(list);
            if (!"删除成功".equals(str)) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return "文件删除失败，请联系管理员";
            }
        }
        int i = newsEntityMapper.deleteByPrimaryKey(newsEntity.getId());
        if (i == 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "删除失败，请联系管理员";
        }
        return "删除成功";
    }

    @Override
    @Transactional
    public String update(NewsEntity newsEntity) {
        NewsEntity entity = newsEntityMapper.selectByPrimaryKey(newsEntity.getId());
        if (!entity.getUrl().equals(newsEntity.getUrl())) {
            List<String> list = new ArrayList<>();
            list.add(entity.getUrl());
            String str = sysFileService.delete(list);
            if (!"删除成功".equals(str)) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return "文件更新失败，请联系管理员";
            }
        }
        int i = newsEntityMapper.updateByPrimaryKeySelective(newsEntity);
        if (i == 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "更新失败，请联系管理员";
        }
        return "更新成功";
    }

    @Override
    public List<NewsFilter> query(NewsFilter filter) {
        List<NewsFilter> list = newsEntityMapper.query(filter);
        for (NewsFilter newsFilter : list) {
            if (StringUtils.isBlank(newsFilter.getUrl())) {
                continue;
            }
            SysFile sysFile = sysFileService.selectOne(Long.valueOf(newsFilter.getUrl()));
            newsFilter.setNum(sysFile.getOldName());
        }
        return list;
    }

    /**
     * 新闻页面查询
     * @param filter
     * @return
     */
    @Override
    public ArrayList<Map<String, Object>> watchNews(NewsFilter filter) {
        List<NewsFilter> list = newsEntityMapper.queryForWatchMonth(filter);
        ArrayList<Map<String, Object>> arrayList = new ArrayList<>();
        Set<String> set = new LinkedHashSet<>();
        //利用set的元素不可重复特性可知道list集合中有几个不同的年份
        for (NewsFilter newsFilter:list) {
            set.add(newsFilter.getDateTime());
        }
        //循环set，先按年份分块，最后放到list中返回
        for (String year:set) {
            Map<String,Object> map = new HashMap<>();
            map.put("dateTime",year);
            //yearList装的该年份内的所有月份数据
            List<Map<String,Object>> yearList = new ArrayList<>();
            for (NewsFilter newsFilter:list){
                //如果list集合中的对象年份与当前年份对应，则放进来
                if (year.equals(newsFilter.getDateTime())){
                    //一下操作把同个月份的新闻放到一起
                    Map<String,Object> maps = new HashMap<>();
                    maps.put("monthTime",newsFilter.getMonthTime());
                    List<NewsFilter> lists = newsEntityMapper.query(newsFilter);
                    for (NewsFilter fil:lists) {
                        if (StringUtils.isNotBlank(fil.getUrl())){
                            SysFile sysFile = sysFileService.selectOne(Long.valueOf(fil.getUrl()));
                            fil.setFileUrl(sysFile.getUrl());
                            fil.setNum(sysFile.getOldName());
                        }
                    }
                    maps.put("list",lists);
                    yearList.add(maps);
                    map.put("list",yearList);
                }
            }
            arrayList.add(map);
        }
        return arrayList;
    }

    @Override
    public Integer queryForCount(NewsFilter filter) {
        return newsEntityMapper.queryForCount(filter);
    }

    /**
     * 获取总的月份
     * @return
     */
    public Integer queryForWatchCount(){
        return newsEntityMapper.queryForWatchCount();
    }

    /**
     * 设置置顶
     * @param newsEntity
     * @return
     */
    @Override
    @Transactional
    public String updateTop(NewsEntity newsEntity) {
        NewsEntity entity = newsEntityMapper.queryForTop();
        if (entity!=null){
            entity.setTop(null);
            int i = newsEntityMapper.updateByPrimaryKey(entity);
            if (i == 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return "置顶失败，请重试";
            }
        }
        newsEntity.setTop("1");
        int a = newsEntityMapper.updateByPrimaryKeySelective(newsEntity);
        if (a == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "置顶失败,请重试";
        }
        return "置顶成功";
    }

    @Override
    public NewsEntity queryTop() {
        return newsEntityMapper.queryForTop();
    }

    /**
     * 首页轮播，取最新5条
     * @return
     */
    @Override
    public List<NewsFilter> shuffling() {
        return newsEntityMapper.shuffling();
    }


}
